package com.ruoyi.score.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.keep.domain.Userlevel;
import com.ruoyi.keep.mapper.UserlevelMapper;
import com.ruoyi.score.domain.ScoreDetail;
import com.ruoyi.score.domain.UserSignIn;
import com.ruoyi.score.mapper.ScoreDetailMapper;
import com.ruoyi.score.mapper.UserSignInMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.score.mapper.ScoreMapper;
import com.ruoyi.score.domain.Score;
import com.ruoyi.score.service.IScoreService;

/**
 * 积分Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-13
 */
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper,Score> implements IScoreService
{
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private ScoreDetailMapper scoreDetailMapper;

    @Autowired
    private UserlevelMapper userlevelMapper;

    @Autowired
    private UserSignInMapper userSignInMapper;

    /**
     * 查询积分
     * 
     * @param id 积分主键
     * @return 积分
     */
    @Override
    public Score selectScoreById(Long id)
    {
        return scoreMapper.selectScoreById(id);
    }

    @Override
    public Map<String,Object> selectScoreByUserId(Long userId) {
        Long scoreToday = scoreDetailMapper.selectTodayScoreByUserId(userId);
        QueryWrapper<ScoreDetail> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",userId).orderByDesc("update_time");
        List<ScoreDetail> scoreDetails = scoreDetailMapper.selectList(wrapper1);
        //查询当前积分和累计积分
        QueryWrapper<Score> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("user_id",userId).eq("del",0);
        Score score = scoreMapper.selectOne(wrapper2);
        Map<String,Object> map = new HashMap<>();
        map.put("scoreToday",scoreToday);
        map.put("scoreDetails",scoreDetails);
        map.put("score",score);
        return map;
    }

    /**
     * 签到，获取，消费积分
     *0是积分增减，1是签到
     * @param score 积分
     * @return 积分
     */
    @Override
    public Integer signIn(Long userId, String note, Long score,Integer flag) {
        if(flag==0){
            int i1;
            if (score>0){
                int i = scoreDetailMapper.insertScoreDetail(new ScoreDetail(score,new Date(), note, userId));
                i1 = scoreMapper.updateScoreByUser(score, userId, 1);
            }else{
                int i = scoreDetailMapper.insertScoreDetail(new ScoreDetail(score,new Date(), note, userId));
                i1 = scoreMapper.updateScoreByUser(score, userId, 2);
            }
            return i1;
        }
        /*
        * 查询是否签到
        * */
        Calendar calendar = Calendar.getInstance();
        Date nowTime = new Date();
        calendar.setTime(nowTime);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        QueryWrapper<UserSignIn> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId)
                .eq("sign_in",1).between("creat_time",zero,nowTime);
        UserSignIn userSignIn = userSignInMapper.selectOne(wrapper);
        if(userSignIn!=null){
            //已签到
            return -1;
        }
            userSignInMapper.insert(new UserSignIn(userId,1,nowTime));
            int i = scoreDetailMapper.insertScoreDetail(new ScoreDetail(score, nowTime, note, userId));
            int i1 = scoreMapper.updateScoreByUser(score, userId, 1);
            //成功返回1，失败0
            return i1;

    }

    /**
     * 查询积分列表
     * 
     * @param score 积分
     * @return 积分
     */
    @Override
    public List<Score> selectScoreList(Score score)
    {
        List<Userlevel> userlevels = userlevelMapper.selectUserlevelList(null);
        List<Score> scores = scoreMapper.selectScoreList(score);
        if (scores != null) {
            for (int i = 0; i < scores.size(); i++) {
                Long scoreTotal = scores.get(i).getScoreTotal();
                for (int j = 1; j < userlevels.size(); j++) {
                    //需要是积分从低到高
                    if(scoreTotal>=userlevels.get(j-1).getScore() && scoreTotal<userlevels.get(j).getScore()){
                        scores.get(i).setUserlevel(userlevels.get(j-1));
                        break;
                    }else if(scoreTotal>=userlevels.get(userlevels.size()-1).getScore()){
                        scores.get(i).setUserlevel(userlevels.get(j));
                        break;
                    }
                }
            }
            List<Score> list=null;
            if(score.getUserlevelId()!=null){
                list = scores.stream().filter(item->item.getUserlevel().getId()==score.getUserlevelId()).collect(Collectors.toList());
                return list;
            }
        }
        return scores;
    }

    /**
     * 查询是否签到
     *
     * @param userId
     * @return 积分
     */
    @Override
    public Integer selectIsSign(Long userId) {
        Calendar calendar = Calendar.getInstance();
        Date nowTime = new Date();
        calendar.setTime(nowTime);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        QueryWrapper<UserSignIn> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId)
                .eq("sign_in",1).between("creat_time",zero,nowTime);
        UserSignIn userSignIn = userSignInMapper.selectOne(wrapper);
        if(userSignIn!=null){
            //已签到
            return 1;
        }
        return 0;
    }

    /**
     * 新增积分
     * 
     * @param score 积分
     * @return 结果
     */
    @Override
    public int insertScore(Score score)
    {
        return scoreMapper.insertScore(score);
    }

    /**
     * 修改积分
     * 
     * @param score 积分
     * @return 结果
     */
    @Override
    public int updateScore(Score score)
    {
        return scoreMapper.updateScore(score);
    }

    /**
     * 批量删除积分
     * 
     * @param ids 需要删除的积分主键
     * @return 结果
     */
    @Override
    public int deleteScoreByIds(Long[] ids)
    {
        return scoreMapper.deleteScoreByIds(ids);
    }

    /**
     * 删除积分信息
     * 
     * @param id 积分主键
     * @return 结果
     */
    @Override
    public int deleteScoreById(Long id)
    {
        return scoreMapper.deleteScoreById(id);
    }
}
